package org.example.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.example.config.PageBean;
import org.example.config.Result;
import org.example.domain.SysDictType;
import org.example.listener.SysDictTypeListener;
import org.example.service.SysDictTypeService;
import org.spring.kun.common.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
@Api(tags = {"字典类型接口"})
@RestController
@RequestMapping("/sysDictType")
public class SysDictTypeController {

    @Autowired
    SysDictTypeService sysDictTypeService;

    @Autowired
    private HttpServletResponse response;

    @RequestMapping("selectList")
    public Result selectList(SysDictType sysDictType, PageBean bean){
        PageHelper.startPage(bean);
        List<SysDictType> list = sysDictTypeService.selectList(sysDictType);
        PageInfo<SysDictType> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @RequestMapping("add")
    public Result add(SysDictType sysDictType){
        sysDictType.setCreateTime(new Date());
       return Result.success(sysDictTypeService.add(sysDictType));
    }


    @RequestMapping("update")
    public Result update(SysDictType sysDictType){
      return Result.success(sysDictTypeService.update(sysDictType));
    }

    @RequestMapping("delete")
   public Result delete(Integer dictId){
        return Result.success(sysDictTypeService.delete(dictId));
   }

   @RequestMapping("execute")
   public void execute(){
      List<SysDictType> list =  sysDictTypeService.list();
       ExcelUtils.exportExcel(list,"列表数据","sheet",response);
   }

   @RequestMapping("list")
   public Result list(){

     List<SysDictType> dict = sysDictTypeService.list();

//       System.out.println(dict);

       TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

       List<Tree<String>> treeNodes = TreeUtil.<SysDictType, String>build(dict, "0", treeNodeConfig,
               (treeNode, tree) -> {
                   tree.setId(treeNode.getDictId()+"");
                   tree.setParentId(treeNode.getDictParentId()+"");

                   // 扩展属性 ...
                   tree.putExtra("dictName", treeNode.getDictName());
                   tree.putExtra("dictType", treeNode.getDictType());
                   tree.putExtra("status", treeNode.getStatus());
               });


       return Result.success(treeNodes);

   }

    /**
     * 根据id查询字典类型
     */

    @RequestMapping("getInfo")
    public Result getInfo(Integer dictId){
        List<SysDictType> list = sysDictTypeService.selectTypeById(dictId);

        return Result.success(list);
    }

    @RequestMapping("delAll")
    public Result delAll(Long[] id){

      return  Result.success(sysDictTypeService.deleteAll(Arrays.asList(id)));
    }


    @RequestMapping("download")
    public void download(HttpServletResponse response) throws IOException {

        List<SysDictType> list = sysDictTypeService.list();

        String fileName=SysDictType.class.getName();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SysDictType.class).sheet("字典信息").doWrite(list);
    }


    @RequestMapping("upload")
    public Map<String,Object> upload(MultipartFile file) throws IOException {

        HashMap<String, Object> map = new HashMap<>();

        try {
            map.put("code",200);
            EasyExcel.read(file.getInputStream(),SysDictType.class, new SysDictTypeListener(sysDictTypeService)).sheet().doRead();
        }catch (IOException e) {
            map.put("code",500);
            e.printStackTrace();
        }

        return map;
    }




}
